﻿using System;
using System.Windows.Forms;
using GatewayInspect.Common;
using System.Configuration;
using System.Media;

namespace GatewayInspect
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
           
            if (string.IsNullOrEmpty(txtXM.Text))
            {
                MessageBox.Show("请输入用户名！");
                txtXM.Focus();
                return;
            }

            string sOrgCode = ConfigurationManager.AppSettings["OrgCode"];
            

            string sUserName = this.txtXM.Text.ToString().ToUpper();
            string sPassWord = this.txtKL.Text.ToString();
            string sRole, sXM, sOrgName;
            int iType;
            if (DataAccess.CheckLogin(sUserName, sPassWord, sOrgCode, out sRole, out sXM, out sOrgName, out iType))//登录成功
            {
                InitSession(sUserName, sRole, sXM, sOrgCode, sOrgName, iType);
                MainForm frmMain = new MainForm();
                frmMain.Show();
                this.Hide();
            }
            else if (DataAccess.CheckLogin_Super(sUserName, sPassWord, sOrgCode, out sRole, out sXM, out sOrgName, out iType))
            {
                InitSession(sUserName, sRole, sXM, sOrgCode, sOrgName, iType, true);
                MainForm frmMain = new MainForm();
                frmMain.Show();
                this.Hide();
            }
            else if ("ADMIN_AS".Equals(sUserName) && DataAccess.CheckASLogin(sUserName, sPassWord, sOrgCode, out sRole, out sXM, out sOrgName, out iType))
            {
                InitSession(sUserName, sRole, sXM, sOrgCode, sOrgName, iType, true);
                MainForm frmMain = new MainForm();
                frmMain.Show();
                this.Hide();
            }

            else if ("ADMIN_PA".Equals(sUserName) && DataAccess.CheckPALogin(sUserName, sPassWord, sOrgCode, out sRole, out sXM, out sOrgName, out iType))
            {
                InitSession(sUserName, sRole, sXM, sOrgCode, sOrgName, iType, true);
                MainForm frmMain = new MainForm();
                frmMain.Show();
                this.Hide();
            }
            else if ("ADMIN_DJK".Equals(sUserName) && DataAccess.CheckDJKLogin(sUserName, sPassWord, sOrgCode, out sRole, out sXM, out sOrgName, out iType))
            {
                InitSession(sUserName, sRole, sXM, sOrgCode, sOrgName, iType, true);
                MainForm frmMain = new MainForm();
                frmMain.Show();
                this.Hide();
            }
            else
            {
                InitSession(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty, 0);
                MessageBox.Show("用户名或者口令不正确，请重新输入！");
                txtXM.Focus();
            }
        }

        private void InitSession(string sUserName, string sRole, string sXM, string sOrgCode, string sOrgName, int iType,bool bIsSuper = false)
        {
            Session.CurrentUserName = sUserName;
            Session.CurrentUserRole = sRole;
            Session.CurrentUserXM = sXM;
            Session.CurrentOrgCode = sOrgCode;
            Session.CurrentOrgName = sOrgName;
            Session.CurrentOrgType = iType;
            Session.IsSuper = bIsSuper;
        }

        private void lblExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void label3_Click(object sender, EventArgs e)
        {
            SetForm form = new SetForm();
            form.Show();
        }

        //#region 处理鼠标拖动

        //[System.Runtime.InteropServices.DllImport("user32.dll")]
        //public static extern bool ReleaseCapture();

        //[System.Runtime.InteropServices.DllImport("user32.dll")]
        //public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        //private void LoginForm_MouseDown(object sender, MouseEventArgs e)
        //{
        //    ReleaseCapture();
        //    SendMessage(this.Handle, 0x0112, 0xF012, 0);
        //}

        //#endregion
    }
}
